//
//  JokeViewController.m
//  项目一
//
//  Created by 芮京 on 2018/9/28.
//  Copyright © 2018 芮京. All rights reserved.
//

#import "JokeViewController.h"
#import "jokeTableViewCell.h"
@interface JokeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *datasource;
@property(nonatomic,strong)NSMutableArray *heightarray;


@end

@implementation JokeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.heightarray=[NSMutableArray new];
    [self.view addSubview:self.tab];
    [self loadData];
    self.datasource=[NSMutableArray new];
    
}

-(UITableView *)tab{
    if (_tab==nil) {
        _tab=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tab.delegate=self;
        _tab.dataSource=self;
    }
    [_tab registerNib:[UINib nibWithNibName:@"jokeTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    return _tab;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    jokeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.datasource.count>0) {
        cell.titlelabel.text=_datasource[indexPath.row];
    }
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [_heightarray[indexPath.row] integerValue];
}


-(void)loadData{
    //显示加载蓝
    
    [SVProgressHUD showWithStatus:@"正在加载...."];
    //创建网络请求
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    //设置默认请求为二进制类型 NSdata
    manger.responseSerializer=[AFHTTPResponseSerializer serializer];
    //数据请求
    [manger GET:JOKE_PATH parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //解析数据
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *array=dic[@"段子"];
       
        for (NSDictionary *dicc in array) {
            [self.datasource addObject:dicc[@"title"]];
            
            //计算单元格的高度  根据label 显示的 字体大小  和  字体样式  来计算
            CGSize size=[dicc[@"title"] boundingRectWithSize:CGSizeMake(WIDTH-40, 1000) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor redColor]} context:nil].size;
            
            [self.heightarray addObject:@(size.height+40)];
            
        }
        //刷新界面
        [self.tab reloadData];
        
        [SVProgressHUD dismiss];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tab.mj_header endRefreshing];
        [self.tab.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        
    }];
    
}
@end
